<?php

namespace addons\Merchants\merapi\modules\v1\controllers;

use Yii;
use yii\data\ActiveDataProvider;
use api\controllers\UserAuthController;
use common\enums\StatusEnum;
use common\models\merchant\CreditsLog;
use common\helpers\BcHelper;

/**
 * 余额记录
 *
 * Class CreditsLogController
 * @package addons\Merchants\merapi\modules\v1\controllers
 * @author YiiFrame <21931118@qq.com>
 */
class CreditsLogController extends UserAuthController
{
    /**
     * @var CreditsLog
     */
    public $modelClass = CreditsLog::class;

    /**
     * 首页
     *
     * @return ActiveDataProvider
     */
    public function actionIndex()
    {
        $start_time = Yii::$app->request->get('start_time');
        $end_time = Yii::$app->request->get('end_time');
        $credit_group = Yii::$app->request->get('credit_group');

        $num_where = [];
        if (!empty($num_type = Yii::$app->request->get('num_type'))) {
            // 1: 增加;2：减少;
            $num_where = $num_type == 1 ? ['>', 'num', 0] : ['<=', 'num', 0];
        }

        return new ActiveDataProvider([
            'query' => $this->modelClass::find()
                ->where(['status' => StatusEnum::ENABLED])
                ->andWhere(['credit_type' => CreditsLog::CREDIT_TYPE_USER_MONEY])
                ->andWhere(['merchant_id' => Yii::$app->user->identity->merchant_id])
                ->andFilterWhere(['between', 'created_at', $start_time, $end_time])
                ->andFilterWhere(['credit_group' => $credit_group])
                ->andFilterWhere($num_where)
                ->orderBy('id desc')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
    }

    /**
     * @return array
     */
    public function actionGroup()
    {
        $start_time = Yii::$app->request->get('start_time', strtotime(date('Y-m-d')));
        $end_time = Yii::$app->request->get('end_time', time());

        $data = $this->modelClass::find()
            ->select(['credit_group', 'sum(num) as num', 'count(id) as count'])
            ->where(['status' => StatusEnum::ENABLED])
            ->andWhere(['credit_type' => CreditsLog::CREDIT_TYPE_USER_MONEY])
            ->andWhere(['merchant_id' => Yii::$app->user->identity->merchant_id])
            ->andFilterWhere(['between', 'created_at', $start_time, $end_time])
            ->groupBy(['credit_group'])
            ->andFilterWhere(['>', 'num', 0])
            ->orderBy('id desc')
            ->asArray()
            ->all();

        $creditGroup = [
            'tinyShopOrdinary' => '普通订单',
            'tinyShopWholesale' => '拼团订单',
            'tinyShopGroupBuy' => '团购订单',
        ];

        $totalMoney = 0;
        foreach ($data as &$datum) {
            $datum['title'] = $creditGroup[$datum['credit_group']] ?? '';
            $totalMoney = BcHelper::add($totalMoney, $datum['num']);
        }

        return [
            'totalMoney' => $totalMoney,
            'list' => $data,
            'memberCount' => Yii::$app->tinyShopService->order->findMemberCountByTime($start_time, $end_time),
            'newOrder' => Yii::$app->tinyShopService->order->findLastPay(),
        ];
    }
}